package eu.jpereira.trainings.designpatterns.creational.builder.json;

import java.util.Iterator;

import eu.jpereira.trainings.designpatterns.creational.builder.model.ReportBody;
import eu.jpereira.trainings.designpatterns.creational.builder.model.ReportBodyBuilder;
import eu.jpereira.trainings.designpatterns.creational.builder.model.SoldItem;

public class JSONReportBodyBuilder implements ReportBodyBuilder
{
	JSONReportBody reportBody;
	boolean itemsAdded;
		
	public JSONReportBodyBuilder()
	{
		reportBody = new JSONReportBody();
		reportBody.addContent("sale:{customer:{");
		itemsAdded = false;
	}
	@Override
	public ReportBodyBuilder setCustomerName(String customerName)
	{
		reportBody.addContent("name:\"" + customerName + "\"");
		return this;
	}

	@Override
	public ReportBodyBuilder setCustomerPhone(String phoneNumber)
	{
		reportBody.addContent(",phone:\"" + phoneNumber + "\"");
		return this;
	}

	@Override
	public ReportBodyBuilder withItems()
	{
		if(!itemsAdded) reportBody.addContent("},items:[");
		else reportBody.addContent("\b]}"); // <-----------------------------------
		return this;
	}

	@Override
	public ReportBodyBuilder newItem(String name, int quantity, double price)
	{
		reportBody.addContent("{name:\"" + name + "\",quantity:" + quantity + ",price:" + price + "},");
		return this;
	}

	@Override
	public ReportBody getReportBody()
	{
		// TODO Auto-generated method stub
		return reportBody;
	}
	@Override
	public void setItemsAdded(boolean b)
	{
		itemsAdded = b;
	}

}
